package com.yonyou.crm.bpub.detail.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.bpub.detail.entity.*;
import com.yonyou.crm.cum.contact.entity.ContactVO;
import com.yonyou.crm.cum.contact.service.ContactBizServiceImpl;
import com.yonyou.iuap.context.InvocationInfoProxy;


@Service
public class ContactDetails extends BaseDetails {

	@Autowired
	ContactBizServiceImpl service;
	
	@Override
	public <T extends Serializable> T getDetail(long id) {
		ContactVO obj = service.getDetail(id);
		if ((obj != null) && (obj.getId() == null)) {
			obj = null;
		}
		return (T)obj;
	}
	
	/**
	 * 获取详情模板
	 */
	@Override
	public  <T extends Serializable> Map<String, List<DetailsGroupVO>> getLayout(T obj) {
		ContactVO vo = (ContactVO) obj;
		//TODO定义biztype
		//Long biztypeId = vo.getBiztypeId();
		long biztypeId = 11;
		return layoutService.getMobileDetailTemplateForCurrentUser(biztypeId);
	}
	
	@Override
	public <T extends Serializable> List<DetailsGroupVO> getHeader(T obj, Map<String, List<DetailsGroupVO>> layoutList) {
		List<DetailsGroupVO> headerlist = new ArrayList<>();
		
		ContactVO vo = (ContactVO)obj;
		DetailsGroupVO header = new DetailsGroupVO();
		List<DetailsItemVO> headerData = new ArrayList<>();
		DetailsItemVO item;
		
		item = new DetailsItemVO();
		item.setItemkey("photo");
		item.setName("头像");
		item.setValue(vo.getPhoto());
		item.setType("pic");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("name");
		item.setName("姓名");
		item.setValue(vo.getName());
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("post");
		item.setName("职位");
		item.setValue(String.valueOf(vo.getPost()));
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("customer");
		item.setName("客户");
		item.setId((vo.getCustomer() == null) ? "" : vo.getCustomer().toString());
		item.setValue((vo.getCustomerInfo() == null || vo.getCustomerInfo().getName() == null) ? "" : vo.getCustomerInfo().getName());
		item.setType("refertype");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("role");
		item.setName("角色");
		item.setId((vo.getRole() == null) ? "" : vo.getRole().toString());
		item.setValue((vo.getRoleName() == null) ? "" : vo.getRoleName());
		item.setType("enum");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("ownerUserId");
		item.setName("负责人Id");
		item.setId((vo.getOwnerUserId() == null) ? "" : vo.getOwnerUserId().toString());
		item.setValue((vo.getOwnerUserInfo() == null || vo.getOwnerUserInfo().getName() == null) ? "" : vo.getOwnerUserInfo().getName());
		item.setType("refertype");
		headerData.add(item);
		
		header.setGroupdata(headerData);
		headerlist.add(header);
		header = new DetailsGroupVO();
		headerData = new ArrayList<>();
		
		item = new DetailsItemVO();
		item.setItemkey("mobile");
		item.setName("手机");
		item.setValue(vo.getMobile());
		item.setType("phone");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("officePhone");
		item.setName("办公电话");
		item.setValue(vo.getOfficePhone());
		item.setType("phone");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("email");
		item.setName("邮箱");
		item.setValue(vo.getEmail());
		item.setType("email");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("deptId");
		item.setName("部门");
		item.setId((vo.getDeptId() == null) ? "" : vo.getDeptId().toString());
		item.setValue((vo.getDeptName() == null) ? "" : vo.getDeptName());
		item.setType("refertype");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("attitude");
		item.setName("态度");
		item.setId((vo.getAttitude() == null) ? "" : vo.getAttitude().toString());
		item.setValue((vo.getAttitudeName() == null) ? "" : vo.getAttitudeName());
		item.setType("enum");
		headerData.add(item);

		item = new DetailsItemVO();
		item.setItemkey("hobby");
		item.setName("兴趣爱好");
		item.setValue((vo.getHobby() == null) ? "" : vo.getHobby());
		item.setType("singlelinetext");
		headerData.add(item);

		item = new DetailsItemVO();
		item.setItemkey("remarks");
		item.setName("备注");
		item.setValue((vo.getRemarks() == null) ? "" : vo.getRemarks());
		item.setType("largetext");
		headerData.add(item);
		
		header.setGroupdata(headerData);
		headerlist.add(header);
		
		return headerlist;
	}

	@Override
	public <T extends Serializable> List<DetailsGroupVO> getBody(T obj, Map<String, List<DetailsGroupVO>> layoutList){
		List<DetailsGroupVO> body = new ArrayList<>();
		return body;
	}
	
	@Override
	public <T extends Serializable> List<DetailsFlagVO> getFlag(T obj){
		List<DetailsFlagVO> flags = new ArrayList<>();
		ContactVO vo = (ContactVO)obj;
		
		DetailsFlagVO flag = new DetailsFlagVO();
		String flagValue = "N";
		flag.setDes("maincontact");
		if ((vo.getMainContact() != null) && (vo.getMainContact() == 1)) {
			flagValue = "Y";
		}
		flag.setValue(flagValue);
		flags.add(flag);
		
		flag = new DetailsFlagVO();
		flagValue = (vo.getFollowFlag().equals("1")) ? "Y" : "N";
		flag.setDes("follow");
		flag.setValue(flagValue);
		flags.add(flag);
		
		return flags;
	}
	
	@Override
	public <T extends Serializable> List<String> getAction(T obj){
		List<String> actions = new ArrayList<>();
		ContactVO vo = (ContactVO) obj;
		Long userId = new Long(InvocationInfoProxy.getParameter("id"));
		Long ownerUserId = vo.getOwnerUserId();
		if (ownerUserId != null){
			if (userId.toString().equals(ownerUserId.toString())) {
				actions.add("edit");
			}
		}
		actions.add("phone");
		actions.add("wechat");
		return actions;
	}

}
